package cn.iocoder.yudao.module.system.dal.mysql.user;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.scores.UserScoresExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.scores.UserScoresPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.UserScoresDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户成绩 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface UserScoresMapper extends BaseMapperX<UserScoresDO> {

    /**
     * 根据用户ID查询当前成绩记录
     *
     * @param userId 用户ID
     * @return 当前成绩记录
     */
    default UserScoresDO selectCurrentByUserId(Long userId) {
        return selectOne(new LambdaQueryWrapperX<UserScoresDO>()
                .eq(UserScoresDO::getUserId, userId)
                .eq(UserScoresDO::getIsCurrent, true));
    }

    /**
     * 根据用户ID查询所有成绩记录
     *
     * @param userId 用户ID
     * @return 成绩记录列表
     */
    default List<UserScoresDO> selectListByUserId(Long userId) {
        return selectList(new LambdaQueryWrapperX<UserScoresDO>()
                .eq(UserScoresDO::getUserId, userId)
                .orderByDesc(UserScoresDO::getCreateTime));
    }

    /**
     * 根据用户ID列表查询当前成绩记录
     *
     * @param userIds 用户ID列表
     * @return 当前成绩记录列表
     */
    default List<UserScoresDO> selectCurrentListByUserIds(List<Long> userIds) {
        return selectList(new LambdaQueryWrapperX<UserScoresDO>()
                .in(UserScoresDO::getUserId, userIds)
                .eq(UserScoresDO::getIsCurrent, true));
    }

    /**
     * 根据省份和考试年份查询成绩记录
     *
     * @param province 省份
     * @param examYear 考试年份
     * @return 成绩记录列表
     */
    default List<UserScoresDO> selectListByProvinceAndYear(String province, Integer examYear) {
        return selectList(new LambdaQueryWrapperX<UserScoresDO>()
                .eq(UserScoresDO::getProvince, province)
                .eq(UserScoresDO::getExamYear, examYear)
                .eq(UserScoresDO::getIsCurrent, true));
    }

    /**
     * 将指定用户的所有成绩记录设置为非当前
     *
     * @param userId 用户ID
     */
    @Update("UPDATE system_user_scores SET is_current = 0 WHERE user_id = #{userId} AND deleted = 0")
    void updateAllToNonCurrent(Long userId);

    // ==================== 控制器相关方法 ====================

    /**
     * 根据用户ID查询成绩记录列表
     *
     * @param userId 用户ID
     * @return 成绩记录列表
     */
    default List<UserScoresDO> selectByUserId(Long userId) {
        return selectList(new LambdaQueryWrapperX<UserScoresDO>()
                .eq(UserScoresDO::getUserId, userId)
                .orderByDesc(UserScoresDO::getCreateTime));
    }

    /**
     * 将指定用户的所有成绩记录设置为非当前
     *
     * @param userId 用户ID
     */
    @Update("UPDATE system_user_scores SET is_current = 0 WHERE user_id = #{userId} AND deleted = 0")
    void updateCurrentToFalse(Long userId);

    /**
     * 根据条件分页查询用户分数
     *
     * @param reqVO 查询条件
     * @return 分页结果
     */
    default PageResult<UserScoresDO> selectPage(UserScoresPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<UserScoresDO>()
                .eqIfPresent(UserScoresDO::getUserId, reqVO.getUserId())
                .likeIfPresent(UserScoresDO::getProvince, reqVO.getProvince())
                .likeIfPresent(UserScoresDO::getGrade, reqVO.getGrade())
                .likeIfPresent(UserScoresDO::getFirstSubject, reqVO.getFirstSubject())
                .likeIfPresent(UserScoresDO::getSecondSubject, reqVO.getSecondSubject())
                .geIfPresent(UserScoresDO::getScore, reqVO.getMinScore())
                .leIfPresent(UserScoresDO::getScore, reqVO.getMaxScore())
                .likeIfPresent(UserScoresDO::getTypeName, reqVO.getTypeName())
                .eqIfPresent(UserScoresDO::getExamYear, reqVO.getExamYear())
                .eqIfPresent(UserScoresDO::getIsCurrent, reqVO.getIsCurrent())
                .betweenIfPresent(UserScoresDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(UserScoresDO::getId));
    }

    /**
     * 根据条件查询用户分数列表
     *
     * @param reqVO 查询条件
     * @return 分数记录列表
     */
    default List<UserScoresDO> selectList(UserScoresExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<UserScoresDO>()
                .eqIfPresent(UserScoresDO::getUserId, reqVO.getUserId())
                .likeIfPresent(UserScoresDO::getProvince, reqVO.getProvince())
                .likeIfPresent(UserScoresDO::getGrade, reqVO.getGrade())
                .likeIfPresent(UserScoresDO::getFirstSubject, reqVO.getFirstSubject())
                .likeIfPresent(UserScoresDO::getSecondSubject, reqVO.getSecondSubject())
                .geIfPresent(UserScoresDO::getScore, reqVO.getMinScore())
                .leIfPresent(UserScoresDO::getScore, reqVO.getMaxScore())
                .likeIfPresent(UserScoresDO::getTypeName, reqVO.getTypeName())
                .eqIfPresent(UserScoresDO::getExamYear, reqVO.getExamYear())
                .eqIfPresent(UserScoresDO::getIsCurrent, reqVO.getIsCurrent())
                .betweenIfPresent(UserScoresDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(UserScoresDO::getId));
    }

}